/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.approve.web;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.utils.excel.ExportExcel;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.approve.entity.HomeApprove;
import com.fosung.frame.modules.approve.service.HomeApproveService;
import com.fosung.frame.modules.life.entity.OrganizeLifeType;
import com.fosung.frame.modules.life.service.OrganizeLifeTypeService;
import com.fosung.frame.modules.sys.utils.DictUtils;
import com.fosung.frame.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;

/**
 * 活动统计Controller
 * @author ygl
 * @version 2017-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/approve/planStatistic")
public class PlanStatisticController extends BaseController {

	@Autowired
	private HomeApproveService homeApproveService;
	@Autowired
	private OrganizeLifeTypeService organizeLifeTypeService;
	
	
	@ModelAttribute
	public HomeApprove get(@RequestParam(required=false) String id) {
		HomeApprove entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = homeApproveService.get(id);
		}
		if (entity == null){
			entity = new HomeApprove();
		}
		return entity;
	}
	
	@RequiresPermissions("approve:planStatistic:view")
	@RequestMapping(value = {"list", ""})
	public String list(HomeApprove homeApprove, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(StringUtils.isEmpty(homeApprove.getYear())) {
			if(DictUtils.findAssessmentTimeList().size()>0) {
				homeApprove.setYear(DictUtils.findAssessmentTimeList().get(0).getValue());
			}else {
				Calendar calendar = Calendar.getInstance();
				homeApprove.setYear(calendar.get(Calendar.YEAR)+"");
			}
		}
		homeApprove.setOrganizationid(UserUtils.getUser().getOffice().getId());
		Page<HomeApprove> page = homeApproveService.findPlanStatisticPage(new Page<HomeApprove>(request, response), homeApprove); 
		String month = homeApprove.getMonth();
		model.addAttribute("page", page);
		model.addAttribute("month", month);
		return "modules/approve/planStatisticList";
	}

	@RequiresPermissions("approve:planStatistic:view")
	@RequestMapping(value = "form")
	public String form(HomeApprove homeApprove, Model model) {
		List<OrganizeLifeType> listStr=new ArrayList<OrganizeLifeType>();
		if (homeApprove.getTypeids() != null && !homeApprove.getTypeids().equals("")) {
			String[] types = homeApprove.getTypeids().split(",");
			
			if (types != null && types.length > 0) {
				for (String s : types) {
					// 循环遍历数组中元素，添加到StringBuilder 对象中
					OrganizeLifeType o=new OrganizeLifeType();
					String t = organizeLifeTypeService.get(s).getTypename();
					o.setTypename(t);
					listStr.add(o);
				}
				model.addAttribute("listStr", listStr);
			}
		}
		model.addAttribute("homeApprove", homeApprove);
		
		Map<Integer, String> yyzlmap = new HashMap<Integer, String>();
		if (homeApprove.getYyzl() != null && !homeApprove.getYyzl().equals("")) {
			String[] yyzl = homeApprove.getYyzl().split("\\|");
			for (int i = 1; i < yyzl.length; i++) {
				yyzlmap.put(i, yyzl[i]);
			}
			model.addAttribute("yyzlmap", yyzlmap);
		}

		Map<String, String> fjmap = new HashMap<String, String>();
		if (homeApprove.getFujian() != null && !homeApprove.getFujian().equals("")) {
			String[] fj = homeApprove.getFujian().split("\\|");
			for (int i = 1; i < fj.length; i++) {
				File tempFile = new File(fj[i].trim());
				String fileName = tempFile.getName();
				try {
					fjmap.put(fj[i], URLDecoder.decode(fileName, "utf-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			model.addAttribute("fjmap", fjmap);
		}
		return "modules/approve/planStatisticForm";
	}
	
	/**
	 * 查询党员排名
	 * 
	 * @param personStatistic
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findPlanStatisticChart")
	@ResponseBody
	public List<HomeApprove> findPlanStatisticChart(HomeApprove homeApprove, Model model) {
		homeApprove.setOrganizationid(UserUtils.getUser().getOffice().getId());
		List<HomeApprove> list = homeApproveService.findPlanStatisticChart(homeApprove);
		return list;
	}

	/**
	 * 导出活动统计Excel
	 * @param homeApprove
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "exportExcel")
	public void exportExcel(HomeApprove homeApprove, HttpServletRequest request, HttpServletResponse response, Model model) {
		homeApprove.setOrganizationid(UserUtils.getUser().getOffice().getId());
		Page<HomeApprove> page = homeApproveService.findPlanStatisticPage(new Page<HomeApprove>(request, response), homeApprove); 
		List<HomeApprove> listInfo = page.getList();
    	
		List<String> headerList = Lists.newArrayList();
		headerList.add("活动名称");
		headerList.add("所属支部");
		headerList.add("分类");
		headerList.add("状态");
		headerList.add("开始时间");
		headerList.add("结束时间");
		headerList.add("活动地点");
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		ExportExcel excel = new ExportExcel("组织活动统计", headerList);
		for (int i = 0; i < listInfo.size(); i++) {
			Row row = excel.addRow();
			HomeApprove obj = listInfo.get(i);
			excel.addCell(row, 0, obj.getLifename(), 2, Class.class);
			excel.addCell(row, 1, obj.getOrganizationName(), 2, Class.class);
			excel.addCell(row, 2, obj.getTypeNames(), 2, Class.class);
			String jhState = "";
			if(obj.getJhstate().equals("3")) {
				jhState = "同意";
			}else if(obj.getJhstate().equals("7")) {
				jhState = "结束";
			}
			excel.addCell(row, 3, jhState, 2, Class.class);
			if(obj.getStarttime() == null) {
				excel.addCell(row, 4, "", 2, Class.class);
			}else {
				excel.addCell(row, 4, format.format(obj.getStarttime()), 2, Class.class);
			}
			if(obj.getEndtime() == null) {
				excel.addCell(row, 5, "", 2, Class.class);
			}else {
				excel.addCell(row, 5, format.format(obj.getEndtime()), 2, Class.class);
			}
			excel.addCell(row, 6, obj.getAdress(), 2, Class.class);
		}
		try {
			excel.write(response,"组织活动统计.xls");
		} catch (IOException e) {
			e.printStackTrace();
		}
		excel.dispose();
	}

}